using System;
using full_leaf_framework.Interact;
using full_leaf_framework.Physics;
using full_leaf_framework.Visual;
using Microsoft.Xna.Framework;
using static full_leaf_framework.Interact.Hud;

namespace hud;

public class CardMenu : Hud {

    public InputManager inputManager;
    public Camera camera;
    public const float gameFrame = 1f / 60f;

    public void StartMenu(InputManager inputManager, Camera camera) {
        this.inputManager = inputManager;
        this.camera = camera;
    }

    public void AnimationForward(IHudUnit hudUnit) {
        float time = hudUnit.GetAnimationTime();
        time = time >= 0f ? time + gameFrame : 0f;
        hudUnit.SetAnimationTime(time);
    }

    public void AnimationBackward(IHudUnit hudUnit) {
        float time = hudUnit.GetAnimationTime();
        time = time <= 1f ? time - gameFrame : 1f;
        hudUnit.SetAnimationTime(time);
    }

    public void ClickOn(IHudUnit hudUnit) {
        var button = (Card)hudUnit;
        Console.WriteLine(button.clickMessage);
    }

    public void ChangeFocus(IHudUnit hudUnit) {
        var button = (Card)hudUnit;
        button.drawable.settledFrame = 1;
    }

    public void ChangeNotFocus(IHudUnit hudUnit) {
        var button = (Card)hudUnit;
        button.drawable.settledFrame = 0;
    }

    public void ButtonActiveEvent(IHudUnit hudUnit) {
        var card = (Card)hudUnit;
        var mouse = inputManager.GetTrackingMouse();
        Vector2 mousePos = mouse.pos.ToVector2();
        mousePos = camera.ReturnPointerPos(mousePos);
        if (ShapeManager.IsCollision(card.collisionBox, new Circle(mouse.pos.ToVector2(), 1))) {
            card.focus?.Invoke(card);
            if (mouse.firedLeft) { card.fired?.Invoke(card); }
            if (mouse.pressedLeft) { card.hold?.Invoke(card);  }
        }
        else {
            card.notFocus?.Invoke(card);
        }
    }

}

public class Card : BasicUnit {

    public string clickMessage = "";

    // 事件列表
    public MenuHandleEvent focus;
    public MenuHandleEvent fired;
    public MenuHandleEvent hold;
    public MenuHandleEvent notFocus;

    public void Translate(Vector2 swiftPos) {
        drawable.pos += swiftPos;
        collisionBox.Translate(swiftPos);
    }

    public override void HandleExtArgus(object[] extArgus) {
        if (extArgus.Length > 0) {
            clickMessage = extArgus[0].ToString();
        }
        else {
            clickMessage = "No Card Message";
        }
    }

    public void SetFrame(int frame) {
        drawable.settledFrame = frame;
    }

}